<!-- 能力素养 -->
<template>
  <!-- <pre>{{ tableData[4] }}</pre> -->
  <a-table
    :columns="columns"
    :data="tableShowData"
    :pagination="false"
    span-all
    stripe
    :row-key="'id'"
  >
    <template #index="{ rowIndex }">
      {{ rowIndex + 1 }}
    </template>
    <template #maxScore>
      {{ upperLimitScore ? upperLimitScore : '无限' }}
    </template>
    <template #scorer="{ rowIndex }">
      <WeightInfo :data="tableData[rowIndex]" />
    </template>

    <template #optional="{ record, rowIndex }">
      <a-tooltip content="编辑">
        <IconFont
          type="icon-edit"
          :size="26"
          :style="{
            marginRight: '10px',
            cursor: 'pointer'
          }"
          @click="goEdit(record, rowIndex)"
        />
      </a-tooltip>
      <a-tooltip content="设置评分人">
        <icon-user-add
          :size="24"
          style="color: #3c6eff"
          @click="setHuman(record, rowIndex)"
        />
      </a-tooltip>
      <a-tooltip content="删除" position="left">
        <a-popconfirm content="是否确认删除该条数据?" @ok="del(rowIndex)">
          <icon-font
            type="icon-delete"
            :size="26"
            :style="{
              cursor: 'pointer',
              marginRight: '10px'
            }"
          />
        </a-popconfirm>
      </a-tooltip>
    </template>
  </a-table>
  <a-button v-if="!props.review" type="text" @click="add">新增指标</a-button>
  <IndexDrawer
    v-model:visible="drawerVisible"
    @submit="submit"
    :data="currData"
  />
  <ScoreHumanModal
    v-model:visible="humanVisible"
    v-model:data="currHumanData"
    @save="saveHUman"
    @del="delHUman"
    :index-type="1"
  />
</template>

<script setup lang="ts">
import { cloneDeep } from 'lodash';
import { PerformanceKeyPerformanceIndexLibraryUse } from '@/apis/management/performance/baseSetting/template/type';
import { PersonnelAbilityQualityIndexLibrary } from '@/apis/human/base/variationManager/probation/type';
import IndexDrawer from './IndexDrawer.vue';
import ScoreHumanModal from './ScoreHumanModal.vue';
import { useVModel } from '@vueuse/core';
import { delExamineToolInfo } from '@/apis/human/base/variationManager/probation';
import WeightInfo from './WeightInfo.vue';

const props = withDefaults(
  defineProps<{
    review?: boolean;
    data?: PerformanceKeyPerformanceIndexLibraryUse[];
    upperLimitScore?: number;
  }>(),
  {
    data: () => []
  }
);
const emit = defineEmits(['update:visible', 'handleOk', 'update:data']);
const tableData = useVModel(props, 'data', emit);
const columns = ref<any>([
  {
    title: '序号',
    dataIndex: '',
    slotName: 'index',
    width: 80
  },
  {
    title: '指标名称',
    dataIndex: 'name',
    width: 200
  },
  {
    title: '指标定义',
    dataIndex: 'definition',
    width: 200
  },

  {
    title: '评分标准',
    dataIndex: 'standard',
    slotName: 'standard',
    children: [
      {
        title: '优秀A',
        dataIndex: 'levelA',
        width: 200
      },
      {
        title: '良好B',
        dataIndex: 'levelB',
        width: 200
      },
      {
        title: '合格C',
        dataIndex: 'levelC',
        width: 200
      },
      {
        title: '较差D',
        dataIndex: 'levelD',
        width: 200
      },
      {
        title: '差E',
        dataIndex: 'levelE',
        width: 200
      }
    ]
  },
  {
    title: '评分上限',
    dataIndex: 'maxScore',
    slotName: 'maxScore',
    width: 100
  },
  {
    title: '指标权重',
    dataIndex: 'indexWeight',
    width: 130
  },
  {
    title: '评分人',
    dataIndex: 'scorer',
    slotName: 'scorer',
    width: 400
  }
]);
onMounted(() => {
  if (!props.review) {
    columns.value.push({
      title: '操作',
      dataIndex: 'optional',
      slotName: 'optional',
      width: 150,
      align: 'center',
      fixed: 'right'
    });
  }
});
const currData = ref<PerformanceKeyPerformanceIndexLibraryUse>({});
const drawerVisible = ref(false);
const editIndex = ref(-1); //编辑项的下标，用以判断新增和编辑，下标小于0是新增数据。大于等于0则是修改此下标的值
const add = () => {
  editIndex.value = -1;
  currData.value = {
    personnelAbilityQualityIndexLibraryScoringStandard: [
      { type: 1, value: void 0 },
      { type: 2, value: void 0 },
      { type: 3, value: void 0 },
      { type: 4, value: void 0 },
      { type: 5, value: void 0 }
    ]
  };
  drawerVisible.value = true;
};
const del = async (index: number) => {
  console.log('index', index);

  if (!tableData.value?.length) return;
  const { id } = tableData.value[index];
  if (id > 0) {
    await delExamineToolInfo({ ids: [id], type: 1 });
  }
  tableData.value.splice(index, 1);
  // change();
};
const goEdit = (
  data: PerformanceKeyPerformanceIndexLibraryUse,
  index: number
) => {
  editIndex.value = index;
  currData.value = data;
  drawerVisible.value = true;
};
const submit = (data: PerformanceKeyPerformanceIndexLibraryUse) => {
  //判断是新增还是编辑
  if (editIndex.value < 0) {
    //新增
    (tableData.value as any).push(data);
  } else {
    //编辑
    if (!tableData.value?.length) return;
    tableData.value[editIndex.value] = data;
  }
  console.log('tableData.value', data);
  console.log('tableData.value', tableData.value);

  // change();
};
const tableShowData = computed(() => {
  tableData.value?.forEach((item: any) => {
    item.personnelAbilityQualityIndexLibraryScoringStandard?.forEach(
      (ele: any) => {
        if (ele.type === 1) {
          item.levelA = ele.value;
        }
        if (ele.type === 2) {
          item.levelB = ele.value;
        }
        if (ele.type === 3) {
          item.levelC = ele.value;
        }
        if (ele.type === 4) {
          item.levelD = ele.value;
        }
        if (ele.type === 5) {
          item.levelE = ele.value;
        }
      }
    );
  });
  return tableData.value;
});
const humanVisible = ref(false);
const currHumanData = ref<PersonnelAbilityQualityIndexLibrary>();
const currentIndex = ref(0);
const setHuman = (data: PersonnelAbilityQualityIndexLibrary, index: number) => {
  data.scorer = data.scorer ? data.scorer : [];
  data.scorerStatus = data.scorerStatus || {
    appoint: { status: 2, weight: 0 },
    superiors: { status: 2, weight: 0 },
    junior: { status: 2, weight: 0 },
    peer: { status: 2, weight: 0 },
    other: { status: 2, weight: 0 }
  };
  console.log('data', data);

  humanVisible.value = true;
  currentIndex.value = index;
  currHumanData.value = cloneDeep(data);
};
const saveHUman = () => {
  if (!tableData.value?.length) return;
  tableData.value[currentIndex.value] = currHumanData.value;
  humanVisible.value = false;
};
const delHUman = () => {
  if (!tableData.value?.length) return;
  tableData.value[currentIndex.value] = currHumanData.value;
  // humanVisible.value = false;
};
</script>

<style lang="less" scoped>
.item {
  display: flex;
  border-bottom: 1px solid #e5e6eb;
  justify-content: space-between;
  align-items: center;
  padding: 5px;
}
.item:last-child {
  border-bottom: none;
}
</style>
